<?php

namespace App\Model\Roles;

use App\Model\BaseModel;

class RolesModel extends BaseModel{

    protected $table = 'roles';

    /**
     * 分页
     * @param int $page
     * @param array $where
     * @param string $columns
     * @param null $perPage
     * @return array
     */
    public function paginate(int $page = 1, $columns = '*', $perPage = null) {

        $perPage = $perPage ?: $this->getPerPage();
        $data = $this->db->withTotalCount()
            ->orderBy('id', 'DESC')
            ->get($this->table, [($page - 1) * $perPage, $perPage], $columns);
        $total = $this->db->getTotalCount();
        $result = [
            'data' => $data,
            'total' => $total,
            'last_page'=>ceil($total/$perPage),
            'per_page'=>$perPage,
            'current_page'=>$page,
        ];
        return $result;
    }

    /**
     * 根据主键ID查询一条数据
     * @param AdminsBean $bean
     * @param string $columns
     * @return AdminsBean|null
     */
    public function find(RolesBean $bean, $columns = '*'):?RolesBean {
        $data = $this->db->where('id', $bean->getId())->getOne($this->table,$columns);
        return empty($data) ? null : new RolesBean($data);
    }














}
